package com.mkvld.appietijdenv2;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;

public class ActPrefsGB extends PreferenceActivity
			implements OnSharedPreferenceChangeListener {
	private static final String[] KEYS = {
		"pref_contract", "pref_loan", "pref_cloud", "budget_period", "budget_year",
		"budget_vacation_budget", "budget_vacation_breeded", "budget_vacation_withdrawn", "budget_vacation_nextyear",
		"budget_adv_budget", "budget_adv_breeded", "budget_adv_withdrawn",
		"budget_tvt_budget", "budget_tvt_breeded", "budget_tvt_withdrawn"
	};
	Preference[] budgetPreferences;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.prefs);
		
		budgetPreferences = new Preference[KEYS.length];
		for (int i = 0; i < budgetPreferences.length; i++) {
			
			budgetPreferences[i] = (Preference) findPreference(KEYS[i]);
			if (KEYS[i].equals("pref_contract")) {
				
				int contract = Integer.parseInt(getPreferenceScreen().getSharedPreferences().getString(KEYS[i], "0"));
				budgetPreferences[i].setSummary(Integer.toString(contract / 4));
				continue;
			} else if (KEYS[i].equals("pref_cloud")
					|| KEYS[i].equals("pref_payday")) {
				continue;
			} else
				budgetPreferences[i].setSummary(getPreferenceScreen().getSharedPreferences().getString(KEYS[i], null));
		}
	}


	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		if (key.equals("pref_contract")) {
			int contract = Integer.parseInt(sharedPreferences.getString(key, "0"));
			findPreference(key).setSummary(Integer.toString(contract / 4));
		} else if (key.equals("pref_payday")) {
			if (sharedPreferences.getBoolean(key, false)) {
				setPaydayNotification();
			} else {
				unsetPaydayNotification();
			}
		} else if (key.equals("pref_cloud")) {
			
		} else
			findPreference(key).setSummary(sharedPreferences.getString(key, null));
		
	}
	@Override
	public void onResume() {
		super.onResume();
		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences()
				.unregisterOnSharedPreferenceChangeListener(this);
	}
	
	private void setPaydayNotification() {
		// TODO implement setPaydayNotification
	}
	
	private void unsetPaydayNotification() {
		// TODO implement unsetPaydayNotification
	}
}
